package com.dy.nygl.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dy.nygl.pojo.EnergyParamter;
import com.dy.nygl.service.EnergyParamterService;

@Controller
@RequestMapping("/paramter")
public class EnergyParamterContorller {

	@Resource(name="energyParamterService")
	private EnergyParamterService energyParamterService;
	
	/**
	 * getEnergyParamter 分页显示能耗参数列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getEnergyParamter")
	public String getEnergyParamter(HttpServletRequest request) {
		
		int currPage = 1;
		int rowsPage = 10;
		int totalRows = energyParamterService.getEnergyParamterCount(); //总行数
		
		int totalPage=totalRows/rowsPage;//总页数
		totalPage=(totalRows%rowsPage)>0?totalPage+1:totalPage;
		
//		System.out.println(totalRows);
		
		if (request.getParameter("currPage")!=null) {
			try { 
				currPage=Integer.parseInt(request.getParameter("currPage"));//当前显示第几页
				if(currPage < 1) {
					currPage = 1;
				} else if(currPage > totalPage) {
					currPage = totalPage;
				}
			} catch(Exception e) { 
				currPage=1;//当前显示第几页
			}
		} else {
			currPage=1;//当前显示第几页
		}		
		
		int begin=(currPage-1)*rowsPage;//计算分页开始行
		int end=currPage*rowsPage-1;
		
		List<EnergyParamter> eplist = energyParamterService.getEnergyParamter(begin, end);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			for(int i=0; i<eplist.size(); i++) {
				Map<String, Object> map = new LinkedHashMap <String, Object>();
				map.put("id", eplist.get(i).getId());
				map.put("name", eplist.get(i).getName());
				map.put("upperlimit", eplist.get(i).getUpperlimit());
				
				list.add(map);					
			}	
		} catch(Exception e) {
			list = null;
		}
		
		request.setAttribute("currPage", currPage);
		request.setAttribute("rowsPage", rowsPage);
		request.setAttribute("totalRows", totalRows);
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("list", list);
			
		return "forward:/paramter/energyparamter.jsp";
	}
	
	/**
	 * addEnergyParamter 添加能耗参数
	 */
	@RequestMapping("/addEnergyParamter")
	public String addEnergyParamter(HttpServletRequest request) {
		String name = request.getParameter("name");
		double upperlimit = Double.parseDouble(request.getParameter("upperlimit"));
		EnergyParamter ep = new EnergyParamter();
		ep.setName(name);
		ep.setUpperlimit(upperlimit);
		
		energyParamterService.addEnergyParamter(ep);
		
		return "redirect:getEnergyParamter.action";
	}
	
	/**
	 * getEnergyParamterById 编辑某个能耗参数时的查询
	 * @param request
	 * @return
	 */
	@RequestMapping("/getEnergyParamterById")
	public String getEnergyParamterById(HttpServletRequest request) {
		int id = Integer.parseInt(request.getParameter("id"));
		EnergyParamter ep = energyParamterService.getEnergyParamterById(id);
		
		request.setAttribute("ep", ep);
		return "forward:/paramter/editenergyparamter.jsp";
	}
	
	/**
	 * updateEnergyParamter 编辑/更新某个能耗参数
	 */
	@RequestMapping("/updateEnergyParamter")
	public String updateEnergyParamter(HttpServletRequest request) {
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		double upperlimit = Double.parseDouble(request.getParameter("upperlimit"));
		EnergyParamter ep = energyParamterService.getEnergyParamterById(id);
		ep.setName(name);
		ep.setUpperlimit(upperlimit);
		
		energyParamterService.updateEnergyParamter(ep);
		return "redirect:getEnergyParamter.action";
	}
	
	/**
	 * deleteEnergyParamter 删除某个能耗参数
	 * @param request
	 */
	@RequestMapping("/deleteEnergyParamter")
	public String deleteEnergyParamter(HttpServletRequest request) {
		int id = Integer.parseInt(request.getParameter("id"));
		energyParamterService.deleteEnergyParamter(energyParamterService.getEnergyParamterById(id));
		return "redirect:getEnergyParamter.action";
	}
	
}
